import {
    axios
} from '../../../utils/ajax.js';
import {
    gettime
} from '../../../utils/djs.js';

Page({
    data: {
        showPopupInvite: true,
        showPopupSuccess: false,
        shareTimes: 0
    },

    onLoad: function(options) {
        let orderNo = options.orderNo;
        let data = {
            orderNo: orderNo,
            ...wx.getStorageSync('userinfo')
        }
        console.log('交易结果入参：', data)
        axios({
            url: '/order/result',
            data: data
        }).then(res => {
            this.setData({
                data: res.data
            })
            console.log('交易结果出参：', res.data)
            if (res.data.isGroup && res.data.groupState == 0) {
                let closetime = this.data.data.closetime;
                this.setData({
                    countdown: gettime(closetime, 3).tips
                })
                let timer = setInterval(() => {
                    if (!gettime(closetime, 3)) {
                        clearInterval(timer)
                        this.setData({
                            countdown: false
                        })
                    } else {
                        this.setData({
                            countdown: gettime(closetime, 3).tips
                        })
                    }
                }, 1000)
            }
            if (res.data.groupState == 1) {
                wx.redirectTo({
                    url: `/pages/home/goodsDetail/grouppage/grouppage?groupId=${this.data.data.groupId}`
                })
            }

        })
    },

    onShareAppMessage: function(res) {
        if (res.from === 'button') {

        }
        this.setData({
            showPopupInvite: false,
            showPopupSuccess: true,
            shareTimes: this.data.shareTimes + 1
        })
        return {
            title: `【仅剩${this.data.data.people}个名额】我${this.data.data.images[0].price}元拼了|${this.data.data.images[0].goodsName}`,
            path: `/pages/home/goodsDetail/grouppage/grouppage?groupId=${this.data.data.groupId}`,
            imageUrl: this.data.data.images[0].url
        }
    },

    hidePopup: function() {
        this.setData({
            showPopupInvite: false,
            showPopupSuccess: false
        })
    },

    prevent: () => {}
})